import time
from algorithm import OriginalSolver, SympySolver
from executor import SingleThreadExecutor, MultiThreadExecutor

def generate_values(start: float, end: float, count: int):
    if count == 1:
        return [(start + end) / 2]
    step = (end - start) / (count - 1)
    return [start + i * step for i in range(count)]

# 定义测试方程，每项为 (描述, 表达式, (起始值, 终止值))
test_equations = [
    ("多项式方程", "x**5 - 3*x**3 + x - 1", (-2, 2)),
    ("指数方程", "exp(x) - 2", (0, 2)),
    ("对数方程", "log(x) + x - 2", (0.1, 2)),
    ("幂函数方程", "x**3 - 8", (0, 5)),
    ("三角函数方程", "sin(x) - 0.5", (0, 3.14))
]

# 重复次数列表
repetitions = [250, 500, 750, 1000, 2000]

# 定义 4 种方案：(求解器名称, 求解器实例, 执行器名称, 执行器实例)
schemes = [
    ("Original", OriginalSolver(), "SingleThread", SingleThreadExecutor()),
    ("Original", OriginalSolver(), "MultiThread", MultiThreadExecutor()),
    ("Sympy", SympySolver(), "SingleThread", SingleThreadExecutor()),
    ("Sympy", SympySolver(), "MultiThread", MultiThreadExecutor())
]

def run_tests():
    precision = 50  # 固定精度
    print("方案\t方程类型\t重复次数\t耗时(秒)")
    for solver_name, solver_instance, exec_name, executor_instance in schemes:
        for desc, expr, (start, end) in test_equations:
            for count in repetitions:
                init_values = generate_values(start, end, count)
                t0 = time.time()
                try:
                    # 执行求解（每个方案都调用对应执行器的 execute 方法）
                    _ = executor_instance.execute(solver_instance, init_values, expr, precision)
                except Exception as e:
                    print(f"{solver_name}/{exec_name}\t{desc}\t{count}\tError: {e}")
                    continue
                elapsed = time.time() - t0
                print(f"{solver_name}/{exec_name}\t{desc}\t{count}\t{elapsed:.4f}")

if __name__ == "__main__":
    run_tests()
